package com.zb.goods.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.result.Result;
import com.zb.service.ZjGoodsSpecService;
import com.zb.utils.TokenUtil;
import com.zb.vo.GoodsSpecSelectVo;
import com.zb.vo.GoodsSpecVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 产品规格表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-08-01
 */
@Api(tags = "商品规格类接口")
@RestController
@RequestMapping("/goods/set/spec")
public class ZjGoodsSpecController {
    @Resource
    private ZjGoodsSpecService goodsSpecService;

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "商品设置-规格 - 新增或修改规格")
    @ApiOperation("新增或修改规格")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid GoodsSpecVo goodsSpecVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        goodsSpecService.save(custAccId, goodsSpecVo);
        return Result.success(null == goodsSpecVo.getId()? "新增规格成功" : "修改规格成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.SELECT, remark = "商品设置-规格 - 获取规格列表")
    @ApiOperation("获取规格列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody GoodsSpecSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取规格列表成功！").data(goodsSpecService.getList(custAccId, selectVo));
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.SELECT, remark = "商品设置-规格 - 获取规格信息")
    @ApiOperation("获取规格信息")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable @ApiParam(value = "规格ID", required = true) Long id) {
        return Result.success("获取规格信息成功").data("row", goodsSpecService.getInfo(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "商品设置-规格 - 批量禁用或启用规格")
    @ApiOperation("批量禁用或启用规格")
    @PutMapping("/disable")
    public Result disable(@RequestParam @ApiParam(value = "状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "规格ID集合", required = true) List<Long> idList) {
        goodsSpecService.updateBatchDisableByIds(idList, status);
        return Result.success(status? "禁用规格成功" : "启用规格成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.GOODS_MODULE_TYPE, type = OperationLogType.DELETE, remark = "商品设置-规格 - 批量删除规格")
    @ApiOperation("批量删除规格")
    @DeleteMapping("/")
    public Result delete(@RequestBody @ApiParam(value = "规格ID集合", required = true) List<Long> idList) {
        goodsSpecService.deleteByIds(idList);
        return Result.success("删除规格成功");
    }

}
